﻿#include "ptmfrm_fifo.h"
#include "cross_com.h"
const int _1SEC_MS = 1000;
const int SPT_CLIENT_NUM = 16;


CFIFO_PTMFrame::CFIFO_PTMFrame()
{
	m_lastTime        = 0;
	m_IFrmDiffSize   =  0;
	memset(&m_frmHead,0,BASE_FRAME_HEAD_LEN);
	m_frmHead.frameTag = RDFrameTag;
	m_frmHead.headLen  = BASE_FRAME_HEAD_LEN;
	m_frmHead.frameRate = 25;
// #ifdef _WIN32
// 	m_frmHead.width = 1024;
// 	m_frmHead.height = 576;
// #else
// 	m_frmHead.width = 1920;
// 	m_frmHead.height = 1080;
// #endif
	m_alignSz       = SIZE_16KB;
	m_alignIdx      = 0;
	m_frameNo       = 0;
#ifdef _DEBUG
	m_bCover        = true;
#else
	m_bCover        = true;
#endif
	CUSTOMER_INFO cInfo;
	for(int i = 0; i < SPT_CLIENT_NUM; i++ )
	{
		cInfo.clientID = i;
		cInfo.consumePos = 0;
		cInfo.bConver  = true;
		cInfo.pts    = 0;
		m_vecCustmer.push_back(cInfo);
	}
	m_alignBuf = NULL;
	m_readFrameNo = 0;
	m_audioCacheSize = 0;
}

CFIFO_PTMFrame::~CFIFO_PTMFrame()
{
	delete [] m_alignBuf;
}

int CFIFO_PTMFrame::Open(int bufSize,int oneframe_sz,int channel)
{
	int destSize = bufSize + FRAME_H_LEN*2;  //默认多加入一个帧头信息的长度
	m_lock.Lock();
	int ret = open(destSize);
	m_frmHead.channel = channel;
	if( NULL == m_alignBuf && oneframe_sz != 0)
	{   //启用内存地址映射
#ifdef _WIN32
		oneframe_sz = SIZE_256KB;
#endif
		m_alignBuf = new uchar_t[oneframe_sz];
		m_alignSz = oneframe_sz ;
	}
	m_alignIdx      = 0;
	m_ch            = channel;
	m_buf_size      = bufSize;

	m_lock.UnLock();
	return ret;
}

int  CFIFO_PTMFrame::OpenReader(int clientID)
{
	CUSTOMER_INFO cInfo;
	cInfo.clientID = clientID;
	if(clientID >= (int)m_vecCustmer.size())
	{
		m_vecCustmer.push_back(cInfo);
	}
	else
	{
		memcpy(&m_vecCustmer[clientID],&cInfo,sizeof(CUSTOMER_INFO));
	}
	return 0;
}

int  CFIFO_PTMFrame::CloseReader(int clientID)
{
	return 0;
}

//针对G726音频做处理, 每秒种40字节， 50帧的数据，避免写入次数过多。 拼接成80字节处理好较好
int	CFIFO_PTMFrame::WriteAudioFrame(uchar_t * buf, uint32_t size)
{
	int ret = 0;

	if(size == 40)
	{
		if(m_audioCacheSize != 0)
		{
			memcpy(m_audioAlignBuf,buf,size);
			m_audioCacheSize = size;
		}
		else
		{  //拼接成80字节一帧再写入，
			memcpy(m_audioAlignBuf + m_audioCacheSize,buf,size);
			m_audioCacheSize += size;
			ret = WriteFrame(buf,size,AFrame);
			m_audioCacheSize = 0;
		}
	}
	else
	{
		ret = WriteFrame(buf,size,AFrame);
	}


	return ret;
}

int	CFIFO_PTMFrame::WriteFrame(uchar_t * buf, uint32_t size,int frameType, int64_t pts, int codeType)
{
	int ret = 0;
	CACHE_FRM_INFO cacheInfo;
	uint32_t len = GetFreeSize();
	uint32_t data_sz = FRAME_H_LEN + size;
	if (data_sz > len ) // 缓存数据长度不足
	{
		if(m_bCover)
		{
			FreeFrameSpace(data_sz);
		}
		else
		{
			return FIFO_ERR_NOTENOUGH;
		}
	}
	if( NULL == m_buf)
	{
		return ERR_FILE_OPEN;
	}

	if(size == 0)
	{
		return ERR_DATA_SIZE;
	}

	m_lock.Lock();
	//获取时间
	int64_t msec = 0;
	struct timeval tv;

	gettimeofday( &tv, NULL );
	msec = (uint64_t)tv.tv_sec * 1000 + (uint64_t)tv.tv_usec / 1000;

	//CROSS_ASSERT(msec >= m_lastTime);

	m_frmHead.frameType = frameType;
	if (pts != 0)
	{
		m_frmHead.pts = m_lastTime = pts;
	}
	else
	{
		m_frmHead.pts = m_lastTime = msec;
	}

	m_frmHead.dataLen   = size;
	m_frmHead.codec_type = codeType;

	if((uint8_t)frameType != AFrame)
	{
		m_frmHead.frameNo = m_frameNo++;
	}

	//写入帧头信息
 	if((uint8_t)frameType == IFrame)
	{
 		cacheInfo.len = data_sz;
		cacheInfo.frameType = frameType;
 		cacheInfo.pos = m_pidx;
		cacheInfo.frmNo = m_frmHead.frameNo;
 		cacheInfo.time = m_lastTime;
		m_lstFrm.push_back(cacheInfo);
	//	if (m_ch == 1)
		{
			//printf("add i frame  size %d  frmNo %d m_lstFrm num %d .\n",
		//	data_sz, cacheInfo.frmNo,m_lstFrm.size());
		}

 	}

	len = m_buf_size - m_pidx;
	//写入数据区信息

	int buf_pos = 0;
	len = m_buf_size - m_pidx;
	if(len < (uint32_t)data_sz)
	{
		if(len < (uint32_t)FRAME_H_LEN)
		{
			//拷贝帧头
			//实际内存已经多分配了一个帧头信息，所以这里不会导致越界
			memcpy(m_buf + m_pidx,&m_frmHead,FRAME_H_LEN);
			memcpy(m_buf,(uchar_t*)&m_frmHead + len , FRAME_H_LEN - len);//尾部镜像技术

			buf_pos = FRAME_H_LEN - len;
			//拷贝数据
			memcpy(m_buf + buf_pos, buf, size);
		}
		else
		{//拷贝帧头
			memcpy(m_buf + m_pidx,&m_frmHead,FRAME_H_LEN);
			//数据分成两段
			len = len - FRAME_H_LEN;
			if( 0 != len)
			{
				memcpy(m_buf + m_pidx + FRAME_H_LEN, buf, len);
			}
			CROSS_ASSERT(size > len);
			memcpy(m_buf, buf + len, size - len);
		}
	}
	else
	{
       // CROSS_ASSERT(m_pidx < m_buf_size);
		memcpy(m_buf + m_pidx,&m_frmHead,FRAME_H_LEN);
		memcpy(m_buf + m_pidx+FRAME_H_LEN, buf, size);
	}

	m_pidx += data_sz;
	if(m_pidx >= m_buf_size)
	{
		m_pidx = m_pidx % m_buf_size;
		m_bOverWrite = true;
	}

	if(m_lstFrm.size() > 20)
	{
		int i = 0;
	//	TRACE("ch %d list num %d need pop front.\n",m_ch,m_lstFrm.size());
		for(i = 0; i < 5; i ++)
		{
			m_lstFrm.pop_front();
		}
	}
// 	if( 1 == m_frmHead.channel &&
// 		IFrame == frameType)
// 	{
// 		PrintTimeMs(msec,0,"IFrame Time");
// 	}
	m_lock.UnLock();
	return ret;
}


int CFIFO_PTMFrame::ReadFrame(uchar_t * buf, int size,int clientID)
{
	CUSTOMER_INFO * pInfo = &m_vecCustmer[clientID];
	if(pInfo->bConver && clientID != 0)
	{  //当发现数据补循环后，会紧跟进最后一个I帧
		m_lock.Lock();
		if( 0 == m_lstFrm.size() )
		{
			pInfo->bConver = false;
			pInfo->consumePos = m_cidx;
			pInfo->bConver = false;
			m_lock.UnLock();
			return ERR_DATA_AREA;
		}
		if(pInfo->pts  < m_lstFrm.front().time )
		{
			pInfo->consumePos = m_lstFrm.back().pos; ;
		}
#ifdef _WIN32
		TRACE("CH%02d data be conver m_readFrameNo %d  IfrmNO[ %d - %d] consumePos %d \n",
			m_ch,m_readFrameNo,m_lstFrm.front().frmNo,m_lstFrm.back().frmNo,pInfo->consumePos);
#endif
		m_lock.UnLock();
		pInfo->bConver = false;
	}

	int data_size = GetDataSize(clientID);
	if(data_size < FRAME_H_LEN)
	{
		return ERR_DATA_AREA;
	}

	m_lock.Lock();

	int diffOffset = 0;
	uint32_t cidx = 0;
	if(clientID == 0)
	{
		cidx = m_cidx;
	}
	else
	{
		cidx = pInfo->consumePos;
	}

	RD_FRAME_H   * pFrm = (RD_FRAME_H * )(m_buf + cidx);
	bool bFound = false;
	if(RDFrameTag != pFrm->frameTag)
	{
		diffOffset = 1;
		int len = m_buf_size - cidx;
		if(len < FRAME_H_LEN)
		{
			memcpy(m_buf + m_buf_size,m_buf,FRAME_H_LEN - len);
		}
		while (diffOffset+FRAME_H_LEN + cidx < m_buf_size)
		{
			if((diffOffset +FRAME_H_LEN) >= data_size)
			{
				break;
			}
			pFrm = (RD_FRAME_H * )(m_buf + cidx + diffOffset);
			if( RDFrameTag == pFrm->frameTag)
			{
				bFound = true;
				break;
			}
			else
			{
				diffOffset++;
			}
		}

		if(!bFound)
		{
			ConsumeFrame(diffOffset,clientID);
			m_lock.UnLock();
			return ERR_DATA_AREA;
		}
		else
		{
			if(0 != diffOffset)
			{
				if( 0 == m_ch)
				{
					//TRACE("consume Frame diffofset %d.\n",diffOffset);
				}
				ConsumeFrame(diffOffset,clientID);
			}
		}
	}

	data_size = GetDataSize(clientID);
	uint32_t frame_size = pFrm->dataLen + FRAME_H_LEN;
	if( frame_size > (uint32_t)data_size)
	{
		m_lock.UnLock();
		return ERR_DATA_AREA;
	}
	if(clientID == 0)
	{
		cidx = m_cidx;
		m_readFrameNo       = pFrm->frameNo;
	}
	else
	{
		cidx = pInfo->consumePos;
		m_readFrameNo       = pFrm->frameNo;
	}
	pInfo->pts = pFrm->pts;
	uint32_t len = m_buf_size - cidx;
	if(len < frame_size)
	{
		memcpy(buf, m_buf + cidx, len);
		memcpy(buf + len, m_buf, frame_size - len);
	}
	else
	{
		memcpy(buf,m_buf + cidx,frame_size);
	}

	m_readFrameNo       = pFrm->frameNo;

	ConsumeFrame(frame_size,clientID);

		m_lock.UnLock();

	return frame_size;

}

int CFIFO_PTMFrame:: close()
{
	m_lock.Lock();
	CFIFO_buf::close();
	m_lstFrm.clear();
	m_lock.UnLock();
	return 0;
}

int CFIFO_PTMFrame::Reset()
{
	m_lock.Lock();
	m_cidx = m_pidx = 0; //复位数据
	m_lstFrm.clear();
	m_bOverWrite = false;
	m_lastTime = 0;
	m_lock.UnLock();

	return 0;
}

int  CFIFO_PTMFrame::SeekTime(int64_t startTime,int prepareSecs,int clientID)
{
	list<CACHE_FRM_INFO>::iterator it;
	int releaseSize = 0;
	
	if(0 == m_lstFrm.size())
	{
		return FIFO_ERR_NODATA;
	}
	if(clientID >= m_vecCustmer.size())
	{
		return ERR_OVER_MAXCLIENT;
	}
	CUSTOMER_INFO * pInfo = &m_vecCustmer[clientID];
	m_lock.Lock();
	
	uint32_t cur_cidx = pInfo->consumePos;
	if(clientID != 0)
	{
		cur_cidx = pInfo->consumePos;
	}
	else
	{
		cur_cidx = m_cidx;
	}
	PrintTimeMs(startTime,0,"Start Recrod Time");
	PrintTimeMs(m_lstFrm.begin()->time,m_lastTime,"prepare Recrod Time");
	TRACE_ERR("Prepare IFrame num %d  data size %d .\n",m_lstFrm.size(),GetDataSize(0));

	if(m_lastTime >= startTime)
	{  //时间反向，则这丢弃缓冲中的数据
		if(clientID != 0)
		{
			pInfo->consumePos = 0;
		}
		else
		{
			m_cidx = 0;
		}
		m_lstFrm.clear();
		m_bOverWrite = false;
		m_lastTime = 0;
		m_lock.UnLock();
		TRACE_ERR("Error prepare record data.\n");
		return FIFO_ERR_NODATA;
	}
	else
	{
		int64_t time = 0;
		for(it = m_lstFrm.begin();it != m_lstFrm.end();)
		{
			cur_cidx   = it->pos;
			time = it->time;
			it++;

			if(cur_cidx >= m_cidx )
			{
				releaseSize = cur_cidx - m_cidx;
			}
			else
			{
				releaseSize = cur_cidx + m_buf_size - m_cidx;
			}
			//可以时间差多出一秒，存储I帧间隔
			if((time + (prepareSecs*_1SEC_MS)) >= startTime)
			{
				break;
			}

			m_lstFrm.pop_front();
		}
		
		if(clientID != 0)
		{
			pInfo->consumePos = cur_cidx; 
		}
		else
		{
			m_cidx = cur_cidx;
		}
	}

	m_lock.UnLock();

	return releaseSize;
}

int CFIFO_PTMFrame::GetDataSizeByTime(int64_t time,int prepareSecs)
{
	SeekTime(time,prepareSecs,0);
	return GetDataSize(0);
}

int CFIFO_PTMFrame::GetWriteBufAddr(uint32_t data_size,FIFO_BUF_INFO * bufInfo)
{
	if (0 == data_size)
	{
		return ERR_DATA_SIZE;
	}
	if(m_alignBuf == NULL)
	{
		return ERR_DATA_BUF_EMPYT;
	}
	m_lock.Lock();
	uint32_t len = m_buf_size - m_cidx;
	if(len < data_size)
	{
		bufInfo->buf = m_alignBuf;
		bufInfo->jump_flag = 1;
	}
	else
	{
		bufInfo->jump_flag = 0;
		bufInfo->buf = m_buf + m_cidx;
	}
	m_lock.UnLock();
	bufInfo->last_frmTm = m_lastTime;
	return 0;
}

int CFIFO_PTMFrame::GetFrameBufAddr(FIFO_BUF_INFO * bufInfo,int clientID)
{
	if(clientID > SPT_CLIENT_NUM)
	{
		return ERR_OVER_MAXCLIENT;
	}
	if(m_alignBuf == NULL)
	{
		return ERR_DATA_BUF_EMPYT;
	}
	CUSTOMER_INFO * pInfo = &m_vecCustmer[clientID];
	if(pInfo->bConver )
	{  //当发现数据补循环后，会紧跟进最后一个I帧
		m_lock.Lock();
		if( 0 == m_lstFrm.size() )
		{
			m_lock.UnLock();
			bufInfo->frame_size = 0;
			bufInfo->buf        = NULL;
			return ERR_DATA_AREA;
		}
		if(m_readFrameNo < m_lstFrm.front().frmNo )
		{
			pInfo->consumePos = m_lstFrm.back().pos; ;
		}
#ifdef _WIN32
       TRACE("CH%02d data be conver m_readFrameNo %d  IfrmNO[ %d - %d] consumePos %d \n",
            m_ch,m_readFrameNo,m_lstFrm.front().frmNo,m_lstFrm.back().frmNo,pInfo->consumePos);
#endif
		m_lock.UnLock();
		pInfo->bConver = false;
	}

	int frmSize = GetFrameSize(clientID);
	if(frmSize < m_alignSz)
	{
		bufInfo->frame_size = 0;
		bufInfo->buf        = NULL;
		return ERR_DATA_AREA;
	}

    m_lock.Lock();
	int diffOffset = 0;
	RD_FRAME_H   * pFrm = (RD_FRAME_H * )(m_buf + pInfo->consumePos);
	bool bFound = false;
	if(RDFrameTag != pFrm->frameTag)
	{
		diffOffset = 1;
		while (diffOffset+FRAME_H_LEN + pInfo->consumePos < m_buf_size)
		{
			if((diffOffset +FRAME_H_LEN) >= frmSize)
			{
				break;
			}
			pFrm = (RD_FRAME_H * )(m_buf + pInfo->consumePos + diffOffset);
			if( RDFrameTag == pFrm->frameTag)
			{
				if(pFrm->frameNo > bufInfo->frameNo)
				{
					bFound = true;
					break;
				}
				diffOffset += pFrm->headLen + FRAME_H_LEN;
			}
			else
			{
				diffOffset++;
			}
		}
		if(!bFound)
		{
			if(0 != m_lstFrm.size())
			{
				pInfo->consumePos = m_lstFrm.back().pos;
			}
			else
			{
				pInfo->consumePos = 0;
				m_lock.UnLock();
				return ERR_DATA_AREA;
			}
		}
		else
		{
			if(0 != diffOffset)
			{
				if( 0 == m_ch)
				{
					//TRACE("consume Frame diffofset %d.\n",diffOffset);
				}
				ConsumeFrame(diffOffset,clientID);
			}
		}
	}

	frmSize = GetFrameSize(clientID);
	bufInfo->frame_size = pFrm->dataLen + FRAME_H_LEN;
	if( bufInfo->frame_size > (uint32_t)frmSize)
	{
		bufInfo->frame_size = 0;
		bufInfo->buf        = NULL;
		m_lock.UnLock();
		return ERR_DATA_AREA;
	}

	if(pFrm->pts == bufInfo->last_frmTm &&
	   pFrm->frameNo == m_readFrameNo)
    {
        ConsumeFrame(pFrm->dataLen + FRAME_H_LEN,clientID);
        bufInfo->frame_size = 0;
		bufInfo->buf        = NULL;
		m_lock.UnLock();
        return ERR_DATA_AREA;
    }

	//给出内存地址
	uint32_t len = m_buf_size - pInfo->consumePos;
	if(bufInfo->frame_size > len)
	{
        bufInfo->jump_flag = true;
		memcpy(m_alignBuf,m_buf  + pInfo->consumePos,len);
		memcpy(m_alignBuf + len,m_buf,bufInfo->frame_size - len);
		bufInfo->buf       = m_alignBuf;
	}
	else
	{
		bufInfo->jump_flag  = false;
		bufInfo->buf        = m_buf  + pInfo->consumePos;
	}

	if( AFrame != pFrm->frameType &&
	    pFrm->frameNo <= m_readFrameNo)
	{

		ConsumeFrame(bufInfo->frame_size,clientID);
        bufInfo->frame_size = 0;
		bufInfo->buf        = NULL;
		m_lock.UnLock();
        return ERR_DATA_AREA;
	}

	m_readFrameNo       = pFrm->frameNo;
	bufInfo->frmType    = pFrm->frameType;
	bufInfo->last_frmTm = pFrm->pts;
	bufInfo->frameNo    = pFrm->frameNo;
	m_lock.UnLock();
#ifdef _WIN32
	CROSS_ASSERT( RDFrameTag == pFrm->frameTag);
#endif
	return bufInfo->frame_size;
}

int CFIFO_PTMFrame:: ConsumeFrame(int frm_sz,int clientID)
{
	if(clientID > SPT_CLIENT_NUM)
	{
		return ERR_OVER_MAXCLIENT;
	}

	if(clientID == 0)
	{
		return ConsumeBuf(frm_sz);
	}

	CUSTOMER_INFO * pInfo = &m_vecCustmer[clientID];
	m_lock.Lock();
	pInfo->consumePos += frm_sz;
	if(pInfo->consumePos > m_buf_size )
	{
		pInfo->consumePos = pInfo->consumePos %m_buf_size;
		//CROSS_ASSERT(pInfo->consumePos <= m_pidx);
	//	TRACE("CH%02d Consume pos %d pidx %d .\n",m_ch,pInfo->consumePos,m_pidx);
	}
	m_lock.UnLock();
	return 0;
}

int CFIFO_PTMFrame:: GetFrameSize(int clientID)
{
	if(m_buf_size == 0)
	{
		return ERR_EMPTY_POINT;
	}
	if(clientID > SPT_CLIENT_NUM)
	{
		return ERR_OVER_MAXCLIENT;
	}
	if(clientID == 0)
	{
		return CFIFO_buf::GetDataSize();
	}

	CUSTOMER_INFO * pInfo = &m_vecCustmer[clientID];
	int data_size = 0;

	//m_lock.Lock();

	//分成 前后两段， [head data] [free][end data]
	if(m_pidx >= pInfo->consumePos)
	{
		data_size = m_pidx - pInfo->consumePos;
	}
	else
	{
		data_size = m_pidx + (m_buf_size - pInfo->consumePos);
	}

	//m_lock.UnLock();
	return data_size;
}

int  CFIFO_PTMFrame::GetDataWater(int clientID)
{
	if(0 == m_buf_size)
	{
		return 0;
	}
	if(0 == clientID)
	{
		return GetCurWarter();
	}
	int frmSize = GetFrameSize(clientID);
	return  (frmSize * 100) / m_buf_size;
}

uint32_t CFIFO_PTMFrame::GetDataSize(int clientID)
{
	uint32_t data_size = 0;
	if( 0 == clientID)
	{
		return CFIFO_buf::GetDataSize();
	}
	else
	{
		CUSTOMER_INFO * pInfo = &m_vecCustmer[clientID];
		m_lock.Lock();

		data_size = (m_pidx + m_buf_size - pInfo->consumePos) % m_buf_size;

		m_lock.UnLock();
	}

	return data_size;

}
//释放数据
int CFIFO_PTMFrame::FreeFrameSpace(uint32_t dataSize)
{
	list<CACHE_FRM_INFO>::iterator it;
	uint32_t releaseSize = 0;
	uint32_t cur_cidx = m_cidx;
	//每次循环覆盖释放1／4的空间
	int give_up_cnt = m_lstFrm.size()/4 ;
	if(give_up_cnt < 2)
	{
		give_up_cnt = 2;
	}

	m_lock.Lock();
	for(it = m_lstFrm.begin();it != m_lstFrm.end();)
	{
		cur_cidx = it->pos;
		if(cur_cidx >= m_cidx )
		{
			releaseSize = cur_cidx - m_cidx;
		}
		else
		{
			releaseSize = cur_cidx + m_buf_size - m_cidx;
		}
		give_up_cnt--;
		CROSS_ASSERT(releaseSize < m_buf_size);
		if(releaseSize >= dataSize && give_up_cnt == 0 )
		{
// 			if(0 == m_ch)
// 			{
// 				TRACE("Free data %d dataSize %d.cur_cidx %d len %d \n",
// 					  releaseSize,dataSize,cur_cidx,it->len);
// 				PrintTimeMs(it->time,0,"cur frame");
// 			}
			ConsumeBuf(releaseSize);
			CROSS_ASSERT(m_cidx == cur_cidx);
			m_cidx = cur_cidx;
			break;
		}
		it++;
		m_lstFrm.pop_front();
	}

#ifdef _SHOW_FRAME_INFO
    if( 4 == m_ch ||
		 6 == m_ch)
    {
		TRACE("Leaf frame cnt %d .\n",m_lstFrm.size());
        for(it = m_lstFrm.begin();it != m_lstFrm.end();it++)
        {
			TRACE("Frm no %d  len %d time %lld",it->frmNo,it->len,it->time);
			PrintTimeMs(it->time,0,"leaf frame");
        }
    }

#endif // _SHOW_FRAME_INFO
	for(int i = 0; i < SPT_CLIENT_NUM; i++)
	{
		CUSTOMER_INFO * pInfo = &m_vecCustmer[i];
		if(pInfo->bConver)
		{
            continue;
		}

		if(0!= m_lstFrm.size())
		{
			if(m_readFrameNo < m_lstFrm.begin()->frmNo )
			{
				pInfo->bConver = true;
				// 			TRACE("1 CH%02d Data is conver consumePos %d m_pidx %d releaseSize %d m_readFrameNo %d Ifrm No %d .\n",
				// 					m_ch,pInfo->consumePos,m_cidx,releaseSize,
				// 					m_readFrameNo,
				// 					m_lstFrm.begin()->frmNo
				// 					);
			}
		}


// 		if(!m_bOverWrite)
// 		{   //被覆盖
// 			if(m_ > pInfo->consumePos)
// 			{
// 				if( 0 == m_ch)
// 				{
// 					TRACE("1 CH%02d Data is conver consumePos %d m_pidx %d releaseSize %d .\n",
// 						m_ch,pInfo->consumePos,m_cidx,releaseSize);
// 				}
// 				pInfo->bConver = true;
// 			}
// 		}
// 		else
// 		{   //回绕时释放的空间是否大于之间剩余
// 			if((m_pidx + m_buf_size)- pInfo->consumePos  < releaseSize)
// 			{
// 				if( 0 == m_ch)
// 				{
// 					TRACE("2 CH%02d Data is conver consumePos %d m_pidx %d releaseSize %d .\n",
// 						m_ch,pInfo->consumePos,m_cidx,releaseSize);
// 				}
// 				pInfo->bConver = true;
// 			}
// 		}
	}
	m_lock.UnLock();
	return 0;
}
